import typing
from datetime import datetime, timedelta
from typing import Optional, List

from bson import ObjectId
from pymongo import MongoClient

from config import settings

client = MongoClient(settings.dsn)

col_inst = client["kexie"]["institution"]
col_activity = client["kexie"]["activity"]
col_register = client["kexie"]["activity_register"]
col_person = client["kexie"]["alumni"]

mapping_region = {
    "东部地区": ["北京市", "天津市", "河北省", "上海市", "江苏", "浙江省", "福建省", "山东省", "广东省", "海南省"],
    "中部地区": ["山西省", "安徽省", "江西省", "河南省", "湖北省", "湖南省"],
    "西部地区": ["内蒙古自治区", "广西壮族自治区", "重庆市", "四川省", "贵州省", "云南省", "西藏自治区", "陕西省",
                 "甘肃省", "青海省", "宁夏回族自治区", "新疆维吾尔自治区"],
    "东北地区": ["辽宁省", "吉林省", "黑龙江省"],
}

identity_values = {
    "中国科协主席": 10,
    "中国科协副主席": 8,
    "中国科协党组成员": 8,
    "中国科协常委": 6,
    "中国科协委员": 4,
    "中国科协代表": 2,
    "省级科协主席": 5,
    "省级科协副主席": 4,
    "省级科协党组成员": 4,
    "省级科协常委": 3,
    "省级科协委员": 2,
    "省级科协代表": 1,
    "全国学会副理事长": 5,
    "全国学会党组成员": 4,
    "全国学会秘书长": 3,
    "全国学会副秘书长": 2,
    "全国学会理事": 1,
    "全国学会理事会党组织负责人": 5,
    "工程师联合体副理事长": 5,
    "工程师联合体党组成员": 4,
    "工程师联合体秘书长": 3,
    "工程师联合体副秘书长": 2,
    "工程师联合体理事": 1,
    "工程师联合体理事会党组织负责人": 5,
    "科创中国青年百人会轮值主席": 3,
    "科创中国青年百人会主席团成员": 2,
    "科创中国青年百人会会员": 1,
    "中国科协党建工作指导委员会主任": 5,
    "中国科协党建工作指导委员会委员": 4,
    "中国科协党建工作指导委员会成员": 2,
    "全国人大代表科协推荐": 8,
    "全国政协委员科协推荐": 8,
}
# 11 部门
ORG_LIST = [ObjectId("62b945cd9aebef53937fecc0"),
            ObjectId("62b945cd9aebef53937fecc1"),
            ObjectId("62b945cd9aebef53937fecc2"),
            ObjectId("62b945cd9aebef53937fecc4"),
            ObjectId("62b945cd9aebef53937fecc5"),
            ObjectId("62b945cd9aebef53937fecc6"),
            ObjectId("62b945cd9aebef53937fecc7"),
            ObjectId("62b945cd9aebef53937fecc8"),
            ObjectId("62b945cd9aebef53937fecc9"),
            ObjectId("62b945cd9aebef53937fecca"),
            ObjectId("62b945cd9aebef53937feccb")]


def query_year(start_year: int = None, end_year: int = None):
    if not start_year:
        start_year = datetime.utcnow().year
    if not end_year:
        end_year = start_year

    from_date = datetime(start_year, 1, 1) - timedelta(hours=8)
    to_date = datetime(end_year + 1, 1, 1) - timedelta(hours=8)
    year_range = {"$gte": from_date, "$lt": to_date}

    return year_range
